package com.jsh.erp.controller;

import com.jsh.erp.datasource.entities.BomMaterial;
import com.jsh.erp.datasource.entities.BomProcess;
import com.jsh.erp.service.BomService;
import com.jsh.erp.utils.BaseResponseInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author : yy
 * @date: 2025/6/3 16:02
 * @description:
 */
@RestController
@RequestMapping(value = "/bom")
@Api(tags = {"bom管理"})
public class BomController {
    private Logger logger = LoggerFactory.getLogger(BomController.class);

    @Resource
    private BomService bomService;

    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "excel表格导入物料")
    public BaseResponseInfo importExcel(@RequestParam("file") MultipartFile file,
                                        @RequestParam("productId") Long productId) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.importExcel(file, productId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return res;
    }

    /**
     * 新增物料
     *
     * @param bomMaterial 物料信息
     * @return 操作结果
     */
    @PostMapping(value = "/addBomMaterial")
    @ApiOperation(value = "新增物料")
    public BaseResponseInfo addBomMaterial(@RequestBody BomMaterial bomMaterial) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.addBomMaterial(bomMaterial);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            res.code = 500;
            res.data = "新增物料失败";
        }
        return res;
    }

    @PutMapping(value = "/updateBomMaterial")
    @ApiOperation(value = "修改物料")
    public BaseResponseInfo updateBomMaterial(@RequestBody BomMaterial bomMaterial) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.updateBomMaterial(bomMaterial);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            res.code = 500;
            res.data = "修改失败";
        }
        return res;
    }

    @PostMapping(value = "/addProductionProcess")
    @ApiOperation(value = "新增生产工序")
    public BaseResponseInfo addProductionProcess(@RequestBody BomProcess bomProcess) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.addProductionProcess(bomProcess);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            res.code = 500;
            res.data = "新增生产工序失败";
        }
        return res;
    }

    @PutMapping(value = "/updateProductionProcess")
    @ApiOperation(value = "修改生产工序")
    public BaseResponseInfo updateProductionProcess(@RequestBody BomProcess bomProcess) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.updateProductionProcess(bomProcess);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            res.code = 500;
            res.data = "修改失败";
        }
        return res;
    }

    /**
     * 批量删除物料
     *
     * @param ids 物料ID列表
     * @return 操作结果
     */
    @PostMapping(value = "/deleteBomMaterials")
    @ApiOperation(value = "批量删除物料")
    public BaseResponseInfo deleteBomMaterials(@RequestBody Long[] ids) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.deleteBomMaterials(ids);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            res.code = 500;
            res.data = "删除失败";
        }
        return res;
    }

    @PostMapping(value = "deleteProductionProcesses")
    @ApiOperation(value = "批量删除生产工序")
    public BaseResponseInfo deleteProductionProcesses(@RequestBody Long[] ids) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.deleteProductionProcesses(ids);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            res.code = 500;
            res.data = "删除失败";
        }
        return res;
    }

    /**
     * 根据生产商品的ID查询物料
     * @param productId 生产商品的ID
     * @return 查询结果
     */
    @GetMapping(value = "/getBomMaterialsByProductId")
    @ApiOperation(value = "根据生产商品的ID查询物料")
    public BaseResponseInfo getBomMaterialsByProductId(@RequestParam("productId") Long productId) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.getBomMaterialsByProductId(productId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return res;
    }

    /**
     * 根据生产商品的ID查询生产工序
     * @param productId 生产商品的ID
     * @return 查询结果
     */
    @GetMapping(value = "/getProductionProcessesByProductId")
    @ApiOperation(value = "根据生产商品的ID查询生产工序")
    public BaseResponseInfo getProductionProcessesByProductId(@RequestParam("productId") Long productId) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            res = bomService.getProductionProcessesByProductId(productId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return res;
    }

    /**
     * 导出物料信息
     *
     * @param productId 生产商品的ID
     * @param response  HTTP响应对象
     */
    @GetMapping(value = "/exportBomMaterials")
    @ApiOperation(value = "导出物料信息")
    public void exportBomMaterials(@RequestParam("productId") Long productId, HttpServletResponse response) {
        try {
            // 调用服务层方法生成文件流
            bomService.exportBomMaterials(productId, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            // 设置错误响应
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.setContentType("text/plain;charset=UTF-8");
            try {
                response.getWriter().write("导出失败：" + e.getMessage());
            } catch (Exception ex) {
                logger.error("写入错误信息失败", ex);
            }
        }
    }
}
